package com.snmp.smn.utils;

import java.util.Date;
import java.util.List;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;

import com.snmp.smn.db.DBHelper;

public class EmailSender {
	
	public static void sendEmail(List<String> addresses, String subject, String body) throws Exception {
		log(addresses, subject, body);
		
		Email email = new SimpleEmail();
		email.setHostName("smtp.gmail.com");
		email.setSmtpPort(587);
		email.setAuthentication("networka79@gmail.com", "Welcome#01");
		email.setTLS(true);
		email.setFrom("networka79@gmail.com", "Network Administrator");
		email.setSubject(subject);
		email.setMsg(body);
		
		for(String address : addresses)
			email.addTo(address);
		
		email.send();
	}

	private static void log(List<String> addresses, String subject, String body) {
		StringBuffer emailContent = new StringBuffer("Sending email :\n");
		emailContent.append("To: ");
		emailContent.append(addresses);
		emailContent.append("\n");
		emailContent.append("Subject: ");
		emailContent.append(subject);
		emailContent.append("\n");
		emailContent.append("Body: \n");
		emailContent.append(body);
		emailContent.append("\n");
		Utilities.log(emailContent.toString());
	}

	public static void sendAgentFailed(int agentId, String agentName,
			String agentIP, int agentPort, String message) throws Exception {
		StringBuffer subject = new StringBuffer(agentName);
		subject.append(" failed at ");
		subject.append(new Date());
		
		StringBuffer body = new StringBuffer("The following agent failed to respond at ");
		body.append(new Date());
		body.append(". \n\n");
		body.append("Agent ID : ").append(agentId).append("\n");
		body.append("Agent Name : ").append(agentName).append("\n");
		body.append("Agent IP : ").append(agentIP).append("\n");
		body.append("Agent Port : ").append(agentPort).append("\n\n");
		body.append("Cause : ").append("\n");
		body.append(message);
		
		sendEmail(DBHelper.getEmailAddresses(), subject.toString(), body.toString());
	}

	public static void sendDeviceFailed(int deviceId, String deviceName,
			String deviceIP, int devicePort, String message) throws Exception {
		StringBuffer subject = new StringBuffer(deviceName);
		subject.append(" failed at ");
		subject.append(new Date());
		
		StringBuffer body = new StringBuffer("The following device failed to respond at ");
		body.append(new Date());
		body.append(". \n\n");
		body.append("Device ID : ").append(deviceId).append("\n");
		body.append("Device Name : ").append(deviceName).append("\n");
		body.append("Device IP : ").append(deviceIP).append("\n");
		body.append("Device Port : ").append(devicePort).append("\n\n");
		body.append("Cause : ").append("\n");
		body.append(message).append("\n\n");
		body.append("Restart command has been issued");
		
		sendEmail(DBHelper.getEmailAddresses(), subject.toString(), body.toString());
	}
	
	public static void sendISNotification(int agentId, String agentName,
								String agentIP, int agentPort) throws Exception {
		StringBuffer subject = new StringBuffer(agentName);
		subject.append(" shutdown improperly");
		
		StringBuffer body = new StringBuffer("The following agent was shutdown improperly.\n\n");
		body.append("Agent ID : ").append(agentId).append("\n");
		body.append("Agent Name : ").append(agentName).append("\n");
		body.append("Agent IP : ").append(agentIP).append("\n");
		body.append("Agent Port : ").append(agentPort).append("\n\n");
		
		sendEmail(DBHelper.getEmailAddresses(), subject.toString(), body.toString());
	}
}